home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 40
/
Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso
/
-readerstuff-
/
iain_hamilton
/
risk.c
< prev
Wrap
C/C++ Source or Header
|
1999-03-27
|
13KB
|
482 lines
/*Risk Probability Calculator
By Iain Hamilton Nov 98
iain@slarti.demon.co.uk
http://www.slarti.demon.co.uk */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define RANDMAX 6
void prob32(void);
void prob31(void);
void prob22(void);
void prob21(void);
void prob11(void);
void clrscr();
main()
{
int exitcode = 0;
char choice;
/* menu code starts here */
while (exitcode == 0)
{
clrscr();
printf("\n\t\tProbability Calculator for Dice Rolls in Risk\n");
printf("\n\t\t\tWritten Nov98 by Iain Hamilton\n");
printf("\n\t\tPlease Choose an option from the menu..\n\n");
printf("\t1...Begin Calculation for 3 attacking vs 2 defending\n\n");
printf("\t2...Begin Calculation for 3 attacking vs 1 defending\n\n");
printf("\t3...Begin Calculation for 2 attacking vs 2 defending\n\n");
printf("\t4...Begin Calculation for 2 attacking vs 1 defending\n\n");
printf("\t5...Begin Calculation for 1 attacking vs 1 defending\n\n");
printf("\t6...Exit the Program\n\n");
printf("\t\tPlease make your choice with the appropriate key : ");
/* Read the Keypress */
choice = getch();
clrscr();
switch (choice)
{
case '1' : prob32();
break;
case '2' : prob31();
break;
case '3' : prob22();
break;
case '4' : prob21();
break;
case '5' : prob11();
break;
case '6' : exitcode = 1;
break;
default : clrscr();
printf("\nChoice out of range, please try again...");
break;
}
printf("\n\n\t\tPress a key to continue....");
getch();
getch();
}
}
/* calculate rolls of the dice and calculate probability */
void prob32(void)
{
/* define variables */
int attackdice1 = 0;
int attackdice2 = 0;
int attackdice3 = 0;
int defenddice1 = 0;
int defenddice2 = 0;
float defwincount = 0;
float attwincount = 0;
float drawcount = 0;
long int throwcount = 0;
int atlodice = 0;
int athidice = 0;
int dehidice = 0;
int delodice = 0;
int startval = 0;
float numthrows = 0;
clrscr();
/* get values from user */
printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
scanf("%d", &startval);
srand(startval);
printf("\n\t\tHow many times shall I roll the dice :");
scanf("%ld", &throwcount);
printf("\n\t\tCalculating for ");
printf("%ld", throwcount);
printf(" Dice Rolls\n");
numthrows = throwcount;
while (throwcount > 0)
{
/* roll the dice */
attackdice1 = (rand()%RANDMAX)+1;
attackdice2 = (rand()%RANDMAX)+1;
attackdice3 = (rand()%RANDMAX)+1;
defenddice1 = (rand()%RANDMAX)+1;
defenddice2 = (rand()%RANDMAX)+1;
/* sort defending dice into highest and lowest */
if (defenddice1 > defenddice2)
{
dehidice = defenddice1;
delodice = defenddice2;
}
else
{
dehidice = defenddice2;
delodice = defenddice1;
}
/* Discard lowest attacking dice and then sort to highest and lowest
(streamlined and hard to follow method (But it works)) */
if (attackdice1 > attackdice2)
{
if (attackdice2 > attackdice3)
{
athidice = attackdice1;
atlodice = attackdice2;
}
else
{
if (attackdice3 > attackdice1)
{
athidice = attackdice3;
atlodice = attackdice1;
}
else
{
athidice = attackdice1;
atlodice = attackdice3;
}
}
}
else
{
if (attackdice1 > attackdice3)
{
athidice = attackdice2;
atlodice = attackdice1;
}
else
{
if (attackdice3 > attackdice2)
{
athidice = attackdice3;
atlodice = attackdice2;
}
else
{
athidice = attackdice2;
atlodice = attackdice3;
}
}
}
/* calculate the winning team */
if (athidice > dehidice && atlodice > delodice)
{
attwincount += 1;
}
else
{
if (athidice <= dehidice && atlodice <= delodice)
{
defwincount += 1;
}
else
{
drawcount += 1;
}
}
throwcount -=1;
}
/* output result */
printf ("\n\n\t\t3 Attacking Dice vs 2 Defending Dice\n");
printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
printf ("\n\t\tDrawn : %0.0f\n\n", drawcount);
printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
printf ("\n\t\tProbability of a Draw taking place is : %0.2f\n",(drawcount/numthrows));
}
void prob31(void)
{
/* define variables */
int attackdice1 = 0;
int attackdice2 = 0;
int attackdice3 = 0;
float defwincount = 0;
float attwincount = 0;
long int throwcount = 0;
int dedice = 0;
int startval = 0;
float numthrows = 0;
clrscr();
/* get values from user */
printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
scanf("%d", &startval);
srand(startval);
printf("\n\t\tHow many times shall I roll the dice :");
scanf("%ld", &throwcount);
printf("\n\t\tCalculating for ");
printf("%ld", throwcount);
printf(" Dice Rolls\n");
numthrows = throwcount;
while (throwcount > 0)
{
/* roll the dice */
attackdice1 = (rand()%RANDMAX)+1;
attackdice2 = (rand()%RANDMAX)+1;
attackdice3 = (rand()%RANDMAX)+1;
dedice = (rand()%RANDMAX)+1;
if (dedice >= attackdice1 && dedice >= attackdice2 && dedice >= attackdice3)
{
defwincount += 1;
}
else
{
attwincount += 1;
}
throwcount -=1;
}
/* output result */
printf ("\n\n\t\t3 Attacking Dice vs 1 Defending Dice\n");
printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
printf ("\n\n\t\tA draw is NOT possible in this situation");
}
void prob22(void)
{
/* define variables */
int attackdice1 = 0;
int attackdice2 = 0;
int defenddice1 = 0;
int defenddice2 = 0;
float defwincount = 0;
float attwincount = 0;
float drawcount = 0;
long int throwcount = 0;
int atlodice = 0;
int athidice = 0;
int dehidice = 0;
int delodice = 0;
int startval = 0;
float numthrows = 0;
clrscr();
/* get values from user */
printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
scanf("%d", &startval);
srand(startval);
printf("\n\t\tHow many times shall I roll the dice :");
scanf("%ld", &throwcount);
printf("\n\t\tCalculating for ");
printf("%ld", throwcount);
printf(" Dice Rolls\n");
numthrows = throwcount;
while (throwcount > 0)
{
/* roll the dice */
attackdice1 = (rand()%RANDMAX)+1;
attackdice2 = (rand()%RANDMAX)+1;
defenddice1 = (rand()%RANDMAX)+1;
defenddice2 = (rand()%RANDMAX)+1;
/* sort defending dice into highest and lowest */
if (defenddice1 > defenddice2)
{
dehidice = defenddice1;
delodice = defenddice2;
}
else
{
dehidice = defenddice2;
delodice = defenddice1;
}
if (attackdice1 > attackdice2)
{
athidice = attackdice1;
atlodice = attackdice2;
}
else
{
athidice = attackdice2;
atlodice = attackdice1;
}
/* calculate the winning team */
if (athidice > dehidice && atlodice > delodice)
{
attwincount += 1;
}
else
{
if (athidice <= dehidice && atlodice <= delodice)
{
defwincount += 1;
}
else
{
drawcount += 1;
}
}
throwcount -=1;
}
/* output result */
printf ("\n\n\t\t2 Attacking Dice vs 2 Defending Dice\n");
printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
printf ("\n\t\tDrawn : %0.0f\n\n", drawcount);
printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
printf ("\n\t\tProbability of a Draw taking place is : %0.2f\n",(drawcount/numthrows));
}
void prob21(void)
{
/* define variables */
int attackdice1 = 0;
int attackdice2 = 0;
float defwincount = 0;
float attwincount = 0;
long int throwcount = 0;
int dedice = 0;
int startval = 0;
float numthrows = 0;
clrscr();
/* get values from user */
printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
scanf("%d", &startval);
srand(startval);
printf("\n\t\tHow many times shall I roll the dice :");
scanf("%ld", &throwcount);
printf("\n\t\tCalculating for ");
printf("%ld", throwcount);
printf(" Dice Rolls\n");
numthrows = throwcount;
while (throwcount > 0)
{
/* roll the dice */
attackdice1 = (rand()%RANDMAX)+1;
attackdice2 = (rand()%RANDMAX)+1;
dedice = (rand()%RANDMAX)+1;
if (dedice >= attackdice1 && dedice >= attackdice2)
{
defwincount += 1;
}
else
{
attwincount += 1;
}
throwcount -=1;
}
/* output result */
printf ("\n\n\t\t2 Attacking Dice vs 1 Defending Dice\n");
printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
printf ("\n\n\t\tA draw is NOT possible in this situation");
}
void prob11(void)
{
/* define variables */
int attackdice1 = 0;
float defwincount = 0;
float attwincount = 0;
long int throwcount = 0;
int dedice = 0;
int startval = 0;
float numthrows = 0;
clrscr();
/* get values from user */
printf("\nEnter a start value for the Pseudorandomizer, between 0 and 5000 :");
scanf("%d", &startval);
srand(startval);
printf("\n\t\tHow many times shall I roll the dice :");
scanf("%ld", &throwcount);
printf("\n\t\tCalculating for ");
printf("%ld", throwcount);
printf(" Dice Rolls\n");
numthrows = throwcount;
while (throwcount > 0)
{
/* roll the dice */
attackdice1 = (rand()%RANDMAX)+1;
dedice = (rand()%RANDMAX)+1;
if (dedice >= attackdice1)
{
defwincount += 1;
}
else
{
attwincount += 1;
}
throwcount -=1;
}
/* output result */
printf ("\n\n\t\t1 Attacking Dice vs 1 Defending Dice\n");
printf ("\n\n\t\tAttacking Won : %0.0f\n", attwincount);
printf ("\n\t\tDefending Won : %0.0f\n", defwincount);
printf ("\n\n\t\tProbability of Attacking Team Winning is : %0.2f",(attwincount/numthrows));
printf ("\n\t\tProbability of Defending Team Winning is : %0.2f",(defwincount/numthrows));
printf ("\n\n\t\tA draw is NOT possible in this situation");
}
void clrscr()
{
/* this required for Compile on Amiga using VBCC, can be removed for Turbo C/++ on PC */
printf ("\f");
}